package me.aartikov.navigationmethodssample;

import me.aartikov.alligator.NavigationContext;
import me.aartikov.alligator.NavigationContextBinder;
import me.aartikov.alligator.Navigator;
import me.aartikov.alligator.Screen;
import me.aartikov.alligator.animations.AnimationData;
import me.aartikov.alligator.animations.SimpleTransitionAnimation;
import me.aartikov.alligator.animations.TransitionAnimation;
import me.aartikov.alligator.helpers.BaseAbility;
import me.aartikov.alligator.annotations.RegisterScreen;

import me.aartikov.alligator.helpers.FractionStack;
import me.aartikov.alligator.screenswitchers.FragmentScreenSwitcher;
import me.aartikov.alligator.screenswitchers.ScreenSwitcher;
import me.aartikov.navigationmethodssample.screens.TestScreen;
import me.aartikov.navigationmethodssample.screens.TestSmallScreen;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

import java.util.List;


@RegisterScreen(TestScreen.class)
public class MainAbility extends BaseAbility implements Component.ClickedListener {
//    @Override
//    public void onStart(Intent intent) {
//        super.onStart(intent);
//        super.setMainRoute(MainAbilitySlice.class.getName());
//    }

    Button forwardButton;
    Button replaceButton;
    Button resetButton;
    Button finishButton;
    Text counterTextView;
    private Navigator mNavigator;
    private NavigationContextBinder mNavigationContextBinder;
    private NavigationContext navigationContext;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        counterTextView = (Text) findComponentById(ResourceTable.Id_counter_text_view);
        forwardButton = (Button) findComponentById(ResourceTable.Id_forward_button);
        replaceButton = (Button) findComponentById(ResourceTable.Id_replace_button);
        resetButton = (Button) findComponentById(ResourceTable.Id_reset_button);
        finishButton = (Button) findComponentById(ResourceTable.Id_finish_button);


        mNavigator = SampleApplication.getInstance().getNavigator();
        mNavigationContextBinder = SampleApplication.getInstance().getNavigationContextBinder();

        TestScreen screen = SampleApplication.getScreenResolver().getScreenOrNull(this);
        int counter = screen != null ? screen.getCounter() : 1;
        counterTextView.setText(getString(ResourceTable.String_counter_template, counter));

        forwardButton.setClickedListener(this);
        replaceButton.setClickedListener(this);
        resetButton.setClickedListener(this);
        finishButton.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        if (SampleApplication.getInstance().doubleClick()) {
            return;
        }
        TestScreen screen = SampleApplication.getScreenResolver().getScreenOrNull(this);
        int counter = screen != null ? screen.getCounter() : 1;
        switch (component.getId()) {
            case ResourceTable.Id_counter_text_view:
                break;
            case ResourceTable.Id_forward_button:
                mNavigator.goForward(new TestScreen(counter + 1, ResourceTable.Id_fragment_container, this.toString()));
                break;
            case ResourceTable.Id_replace_button:
                mNavigator.replace(new TestScreen(counter, ResourceTable.Id_fragment_container, this.toString()));
                break;
            case ResourceTable.Id_reset_button:
                mNavigator.reset(new TestScreen(1, ResourceTable.Id_fragment_container, this.toString()));
                break;
            case ResourceTable.Id_finish_button:
                mNavigator.finish();
                break;
        }
    }


    private void bindNavigationContext() {
        ScreenSwitcher mScreenSwitcher = new FragmentScreenSwitcher(SampleApplication.getNavigationFactory(), ((FractionAbility) this).getFractionManager(), new FragmentScreenSwitcher.AnimationProvider() {
            @Override
            public TransitionAnimation getAnimation(Screen screenFrom, Screen screenTo, AnimationData animationData) {
                return new SimpleTransitionAnimation(0, 0);
            }
        });
        navigationContext = new NavigationContext.Builder(this, SampleApplication.getInstance().getNavigationFactory())
                .fragmentNavigation(getFractionManager())
                .screenSwitcher(mScreenSwitcher)
                .transitionAnimationProvider(new SampleTransitionAnimationProvider())
                .build();
        mNavigationContextBinder.bind(navigationContext);
    }

    public void setInitialFragmentIfRequired() {
        List<Screen> screens = FractionStack.getInstance(getFractionManager()).getCurrentFragmentScreen(this.toString());
        if (screens.size() == 0 && mNavigator.canExecuteCommandImmediately()) {
            mNavigator.reset(new TestSmallScreen(1, ResourceTable.Id_fragment_container, this.toString()));
        } else {
            mNavigator.reduce(screens);
        }
    }

    @Override
    public void onActive() {
        super.onActive();
        bindNavigationContext();
        setInitialFragmentIfRequired();
    }

    @Override
    protected void onBackPressed() {
        if (!mNavigator.goBack()) {
            super.onBackPressed();
        }
    }

    @Override
    protected void onInactive() {
        mNavigationContextBinder.unbind(this);
        super.onInactive();
    }


}
